package jfox.platform.sample;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;

import jfox.platform.common.JSONUtils;
import jfox.platform.infrastructure.EntityObject;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.ActionSupport;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
@Service(id = "sample")
public class SampleAction extends ActionSupport {

    public static final String PAGE_THEME_KEY = "J_THEME";
    public static final String PAGE_JSONUTILS_KEY = "J_JSONUTILS";
    public static final String PAGE_CONSTANTS_KEY = "J_CONSTANTS";
    public static final String PAGE_MODULES_KEY = "J_MODULES";
    public static final String PAGE_MODULE_KEY = "J_MODULE";
    public static final String PAGE_NODE_KEY = "J_NODE";
    public static final String PAGE_MENU_NODES_KEY = "J_MENU_NODES";
    public static final String PAGE_BUTTON_NODE_GROUPS = "J_BUTTON_NODE_GROUPS";


    protected void preAction(ActionContext actionContext) {
        super.preAction(actionContext);

        PageContext pageContext = actionContext.getPageContext();
        // set common attribute
        pageContext.setAttribute(PAGE_JSONUTILS_KEY, JSONUtils.getInstance());
//        pageContext.setAttribute(PAGE_CONSTANTS_KEY, Constants.getInstance());
    }

    @ActionMethod(name="index", successView = "sample/ext_index.vhtml")
    public void indexPage(ActionContext actionContext) throws Exception {

    }

    @ActionMethod(name = "extbasictree", successView = "sample/ext_basic_tree.vhtml")
    public void extTreeExample(ActionContext actionContext) throws Exception {
        List<ExampleEntity> exampleTreeNodes = new ArrayList<ExampleEntity>();

        int rootNodeCounts = 0;
        while (rootNodeCounts < 1) {
            rootNodeCounts = (int)(Math.random() * 10);
        }
        for (int count = 0; count < rootNodeCounts; count++) {
            ExampleEntity root = new ExampleEntity();
            root.setName("TREE_NODE_" + root.getId());
            exampleTreeNodes.add(root);

            int childCounts = 0;
            while (childCounts < 1) {
                childCounts = (int)(Math.random() * 10);
            }
            for (int c = 0; c < childCounts; c++) {
                ExampleEntity child = new ExampleEntity();
                child.setName("TREE_NODE_" + child.getId());
                child.setParentId(root.getId());
                exampleTreeNodes.add(child);
                Thread.sleep(10);
            }
        }

        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("exampleTreeNodes", exampleTreeNodes);

    }

    @ActionMethod(name = "extcolumntree", successView = "sample/ext_column_tree.vhtml")
    public void extColumnTreeExample(ActionContext actionContext) throws Exception {
        List<ExampleEntity> exampleTreeNodes = new ArrayList<ExampleEntity>();

        int rootNodeCounts = 0;
        while (rootNodeCounts < 1) {
            rootNodeCounts = (int)(Math.random() * 10);
        }
        for (int count = 0; count < rootNodeCounts; count++) {
            ExampleEntity root = new ExampleEntity();
            root.setName("TREE_NODE_" + root.getId());
            exampleTreeNodes.add(root);

            int childCounts = 0;
            while (childCounts < 1) {
                childCounts = (int)(Math.random() * 10);
            }
            for (int c = 0; c < childCounts; c++) {
                ExampleEntity child = new ExampleEntity();
                child.setName("TREE_NODE_" + child.getId());
                child.setParentId(root.getId());
                exampleTreeNodes.add(child);
                Thread.sleep(10);
            }
        }

        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("exampleTreeNodes", exampleTreeNodes);

    }

    @ActionMethod(name = "extbasicgrid", successView = "sample/ext_basic_grid.vhtml")
    public void extBasicGridExample(ActionContext actionContext) throws Exception {
        List<ExampleEntity> exampleEntities = new ArrayList<ExampleEntity>();
        for(int i = 0; i<200; i++){
            ExampleEntity ee = new ExampleEntity();
            ee.setName("NAME_" + ee.getId());
            exampleEntities.add(ee);
        }
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("exampleEntities", exampleEntities);
    }

    //TODO: 未实现，等待 ext 2.0 正式版
    @ActionMethod(name = "exteditgrid", successView = "sample/ext_edit_grid.vhtml")
    public void extEditGridExample(ActionContext actionContext) throws Exception {

    }

    public static class ExampleEntity extends EntityObject {
        @Column(name = "PARENT_ID")
        private long parentId;

        public long getParentId() {
            return parentId;
        }

        public void setParentId(long parentId) {
            this.parentId = parentId;
        }
    }

    public static void main(String[] args) {

    }
}
